package com.cf.data.serve.environment;

import com.cf.data.serve.entity.HjjcData;
import com.cf.data.serve.service.HjjcDataService;
import com.cf.data.serve.utils.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import rk.netDevice.sdk.p2.*;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Slf4j
public class TCPServer_yc  {
    private static HjjcDataService hjjcDataService = BeanUtils.getBean(HjjcDataService.class);

    public void yc(int prot) throws InterruptedException {
        System.out.println("开启端口：8335");
        RSServer rsServer;
        rsServer = RSServer.Initiate(prot);
        rsServer.addDataListener(new IDataListener() {
            @Override
            public void receiveTimmingAck(TimmingAck data) {
                // TODO 校时指令应答处理
                System.out.println("校时应答->设备编号:" + data.getDeviceId() + "\t执行结果："
                        + data.getStatus() + "\r\n");
            }
            @Override
            public void receiveTelecontrolAck(TelecontrolAck data) {
                // TODO 遥控指令应答处理
                System.out.println("遥控应答->设备编号:" + data.getDeviceId() + "\t继电器编号:"
                        + data.getRelayId() + "\t执行结果:" + data.getStatus() + "\r\n");
            }
            @Override
            public void receiveStoreData(StoreData data) {
                // TODO 已存储数据接收处理
                // �����ڵ����ݡ����ݰ��������豸�������Լ������ڵ����ݡ���ʪ�����ݴ���ڽڵ�������
                for (NodeData nd : data.getNodeList()) {
                    SimpleDateFormat sdf = new SimpleDateFormat(
                            "yy-MM-dd HH:mm:ss");
                    String str = sdf.format(nd.getRecordTime());
                    System.out.println("存储数据->设备地址:" + data.getDeviceId() + "\t节点:"
                            + nd.getNodeId() + "\t温度:" + nd.getTem() + "\t湿度:"
                            + nd.getHum() + "\t存储时间:" + str+"\t坐标类型："+nd.getCoordinateType()+"\t经度:"+nd.getLng()+"\t纬度："+nd.getLat() + "\r\n");
                }
            }
            @Override
            public void receiveRealtimeData(RealTimeData data) {
                // TODO 实时数据接收处理
                HjjcData hjjcSbgl =new HjjcData();
                hjjcSbgl.setMn(String.valueOf(data.getDeviceId()));
                hjjcSbgl.setId(String.valueOf(System.currentTimeMillis()));
                hjjcSbgl.setState("0");
                hjjcSbgl.setAddTime(new Date());
                DecimalFormat df = new DecimalFormat("0.0");
                for (NodeData nd : data.getNodeList()) {
/*                    System.out.println("\t实时数据->设备地址:" + data.getDeviceId() + "\t节点:"
                            + nd.getNodeId() + "\t温度:" + nd.getTem() + "\t湿度:"
                            + nd.getHum() + "\t经度：" + data.getLng() + "\t纬度："
                            + data.getLat() + "\t坐标类型：" + data.getCoordinateType()
                            + "\t继电器状态" + data.getRelayStatus() + "\t浮点型数据："
                            + nd.getFloatValue() + "\t32位有符号数据："
                            + nd.getSignedInt32Value() + "\t32位无符号数据："
                            + nd.getUnSignedInt32Value() + "\r\n");*/
                    switch (nd.getNodeId()){
                        case 1:
                            System.out.println("PM10："+nd.getTem()*10+"，PM2.5："+nd.getTem()*10);
                            hjjcSbgl.setPm10(String.valueOf(nd.getTem()*10));
                            hjjcSbgl.setPm25(String.valueOf(nd.getTem()*10));
                            break;
                        case 2:
                            System.out.println("噪声："+df.format(nd.getHum()));
                            hjjcSbgl.setZy(df.format(nd.getHum()));
                            break;
                        case 3:
                            System.out.println("温度："+df.format(nd.getTem())+"，湿度："+df.format(nd.getHum()));
                            hjjcSbgl.setTem(df.format(nd.getTem()));
                            hjjcSbgl.setRh(df.format(nd.getHum()));
                            break;
                        case 4:
                            System.out.println("风级："+nd.getTem()*10+"，风速："+df.format(nd.getHum()));
                            hjjcSbgl.setFj(String.valueOf(nd.getTem()*10));
                            hjjcSbgl.setWs(String.valueOf(nd.getHum()*10));
                            break;
                        case 5:
                            System.out.println("风向："+nd.getHum()*10);
                            hjjcSbgl.setFx(String.valueOf(nd.getHum()*10));
                            break;
                        case 6:
                            System.out.println("风向（度数）："+nd.getHum()*10);
                            hjjcSbgl.setWd(String.valueOf(nd.getHum()*10));
                            break;
                        case 7:
                            System.out.println("TSP："+nd.getHum()*10);
                            hjjcSbgl.setTsp(String.valueOf(nd.getHum()*10));
                            break;
                        case 8:
                            System.out.println("气压："+df.format(nd.getHum()));
                            hjjcSbgl.setPre(df.format(nd.getHum()));
                            break;
                    }
                }
                hjjcDataService.save(hjjcSbgl);
            }
            @Override
            public void receiveLoginData(LoginData data) {
                // TODO 设备登录处理
                System.out.println("��¼->�豸��ַ:" + data.getDeviceId());
            }
            @Override
            public void receiveParamIds(ParamIdsData data) {
                // TODO 设备参数编号接收处理
                String str = "设备参数编号列表->设备编号：" + data.getDeviceId() + "\t参数总数量："
                        + data.getTotalCount() + "\t本帧参数数量：" + data.getCount()
                        + "\r\n";
                for (int paramId : data.getPararmIdList())// �����豸�в���id���
                {
                    str += paramId + ",";
                }
                System.out.println(str);
            }

            @Override
            public void receiveParam(ParamData data) {
                // TODO 设备参数接收处理
                String str = "�豸����->�豸��ţ�" + data.getDeviceId() + "\r\n";

                for (ParamItem pararm : data.getParameterList()) {
                    str += "参数编号："
                            + pararm.getParamId()
                            + "\t参数描述："
                            + pararm.getDescription()
                            + "\t参数值："
                            + (pararm.getValueDescription() == null ? pararm
                            .getValue() : pararm.getValueDescription().get(
                            pararm.getValue())) + "\r\n";
                }
                System.out.println(str);
            }
            @Override
            public void receiveWriteParamAck(WriteParamAck data) {
                // TODO 下载参数后设备应答处理
                String str = "下载设备参数->设备编号：" + data.getDeviceId() + "\t参数数量："
                        + data.getCount() + "\t"
                        + (data.isSuccess() ? "下载成功" : "下载失败");
                System.out.println(str);
            }
            @Override
            public void receiveTransDataAck(TransDataAck data) {
                // TODO 透传数据后设备应答处理
                String str = "数据透传->设备编号：" + data.getDeviceId() + "\t响应结果："
                        + data.getData() + "\r\n字节数：" + data.getTransDataLen();
                System.out.println(str);
            }

            @Override
            public void receiveHeartbeatData(HeartbeatData heartbeatData) {

            }
        });
        rsServer.start();

    }
}
